import { ref, computed, onUnmounted } from "vue"
import dayjs from 'dayjs';
export const useCountDown = () => {

    //1.创建响应式倒计时数据
    const time = ref(0)
    const formatTime = computed(() => dayjs.unix(time.value).format('mm分ss秒'))
    //2.创建倒计时函数
    let timer = {}
    const start = (curTime) => {
        time.value = curTime
        timer = setInterval(() => {
            time.value--;
        }, 1000)
    }
    // 组件销毁时清除定时器
    onUnmounted(() => {
        timer && clearInterval(timer)
    })



    return {
        time,
        formatTime,
        start
    }
}


